package com.example.yunxinagyue

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

//构建歌曲播放列表的适配器

var music_num=0
class MusicListAdapter(private val musicList:List<Music>): RecyclerView.Adapter<MusicListAdapter.ViewHolder>()
{
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
        val num: TextView =view.findViewById(R.id.num)
        val musicName: TextView =view.findViewById(R.id.music_name)
        val musicPlay: TextView =view.findViewById(R.id.music_play)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view= LayoutInflater.from(parent.context).inflate(R.layout.music_list_item,parent,false)
//        val viewHolder=ViewHolder(view)
//        viewHolder.itemView.setOnClickListener(){
//            var position=viewHolder.absoluteAdapterPosition
//        }
        return ViewHolder(view)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val music=musicList[position]
        holder.num.text=music.num
        holder.musicName.text=music.musicName
        holder.musicPlay.text="${music.play}—${music.album}"
        holder.itemView.setOnClickListener { view ->
            if (mOnItemClickListener != null) {
                mOnItemClickListener!!.onItemClick(view, position)
            }
        }
    }

    override fun getItemCount(): Int {
        return musicList.size
    }


//    在适配器中定义点击元素的接口和包含的方法，点击事件和长点击事件
    interface OnItemClickListener {
        fun onItemClick(view: View?, position: Int)
        fun onItemLongClick(view: View?, position: Int)
    }

    private var mOnItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnItemClickListener = onItemClickListener
    }


}